package com.ruoyi.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.article.domain.ZAttention;
import com.ruoyi.article.domain.vo.AppUserInfo;
import com.ruoyi.article.mapper.ZAattentionMapper;
import com.ruoyi.article.service.IZAattentionService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ZAattentionServiceImpl extends ServiceImpl<ZAattentionMapper, ZAttention> implements IZAattentionService {

    @Autowired
    private ISysUserService userService;
    @Override
    public Boolean isPassivity(ZAttention zAattention) {
        LambdaQueryWrapper<ZAttention> zAattentionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        zAattentionLambdaQueryWrapper.eq(ZAttention::getPassivityId,zAattention.getPassivityId())
                .eq(ZAttention::getUserId,zAattention.getUserId());
        ZAttention one = getOne(zAattentionLambdaQueryWrapper);
        if (one == null){
            //未关注
            return false;
        }else {
            //以前关注过，但是取消了的情况
            if (!one.getStatus()){
                return false;
            }
            return true;
        }
    }

    @Override
    public int passivity(ZAttention zAattention) {
        Boolean passivity = isPassivity(zAattention);

        LambdaQueryWrapper<ZAttention> zAattentionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        zAattentionLambdaQueryWrapper.eq(ZAttention::getPassivityId,zAattention.getPassivityId())
                .eq(ZAttention::getUserId,zAattention.getUserId());
        ZAttention one = getOne(zAattentionLambdaQueryWrapper);
        //已关注，取消关注
        if (passivity){
            one.setStatus(false);
            updateById(one);
        }else {
            if (ObjectUtils.isNull(one)) {
                //第一次关注
                zAattention.setStatus(true);
                save(zAattention);
            }else {
                //以前关注过，但是取消了的情况
                one.setStatus(true);
                updateById(one);
            }

        }
        return 1;
    }

    @Override
    public List<AppUserInfo> getUserList(Long id) {
        LambdaQueryWrapper<ZAttention> zAttentionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        zAttentionLambdaQueryWrapper.eq(ZAttention::getUserId,id);
        List<ZAttention> zAttentions = list(zAttentionLambdaQueryWrapper);
        if (ObjectUtils.isEmpty(zAttentions)){
            throw new ServiceException("未查询到关注列表");
        }
        ArrayList<AppUserInfo> appUserInfos = new ArrayList<>();
        for (ZAttention zAttention : zAttentions) {
            Long passivityId = zAttention.getPassivityId();
            SysUser sysUser = userService.selectUserById(passivityId);

            if (ObjectUtils.isNotNull(sysUser)){
                AppUserInfo appUserInfo = new AppUserInfo();
                appUserInfo.setUserId(sysUser.getUserId());
                appUserInfo.setUserName(sysUser.getUserName());
                appUserInfo.setAvatar(sysUser.getAvatar());
                appUserInfo.setStatus(zAttention.getStatus());
                appUserInfos.add(appUserInfo);
            }
        }
        return appUserInfos;
    }
}
